import { Singleton } from "./Singleton";
import { MineItem } from "./MineItem";
import { ShopItemManager } from "./ShopItemManager";


export class GameManager<T> extends Singleton<T> {

    public targetMoneyLabel: cc.Label; //目标钱数
    public currentMoneyLabel: cc.Label; //当前钱数
    public endGamePanel: cc.Node; //结束游戏显示panel
    public dialogPanel:cc.Node;
    levelLabel: cc.Label;
    timeLabel: cc.Label;
    shopPanel: cc.Node;

    btnLabel: cc.Label;
    tipLabel: cc.Label;

    public money = 0; //得分

    public catchItem: MineItem = null;

    public ropeSpeed; //绳子速度

    public remainTime = 60; //剩余时间

    public mineCar: cc.Node; //矿车

    public normalRopeSpeed: number = 100;//绳子正常速度

    public ropeState: number; //绳子伸缩状态

    public hookItem: cc.Node; //钩子碰撞主体

    moneyRequired = [0, 60, 500, 1000, 1500, 2000, 2500, 3000, 3500];

    public timer;

    ROPE_STATE = cc.Enum({
        ADD: 1,
        REDUCE: 2,
        WAIT: 3
    });

    level: number = 0;
    public minerAnimation: cc.Animation;

    constructor() {
        super();

        this.targetMoneyLabel = cc.find("UI/goal-money-banner/targetMoneyLabel").getComponent(cc.Label);
        this.currentMoneyLabel = cc.find("UI/goal-money-banner/currentMoneyLabel").getComponent(cc.Label);
        this.levelLabel = cc.find("UI/level-time-banner/levelLabel").getComponent(cc.Label);
        this.timeLabel = cc.find("UI/level-time-banner/timeLabel").getComponent(cc.Label);

        this.endGamePanel = cc.find("UI/dialog-background");
        this.btnLabel = cc.find("button/btnLabel", this.endGamePanel).getComponent(cc.Label);
        this.tipLabel = cc.find("tipLabel", this.endGamePanel).getComponent(cc.Label);

        this.targetMoneyLabel.string = '0';
        this.level = cc.sys.localStorage.getItem('level') || 1;
        if (this.level > this.moneyRequired.length - 1) {
            this.level = this.moneyRequired.length - 1;
        }
        this.targetMoneyLabel.string = this.moneyRequired[this.level].toString();
        this.levelLabel.string = this.level.toString();
        this.minerAnimation = cc.find("minerCar").getComponent(cc.Animation);
        this.shopPanel = cc.find("shop-panel");
        this.hookItem = cc.find("minerCar/Hook/item");
    }

    /**
     * 增加金钱
     * @param money 
     */
    public increseMoney(money: number = 10) {
        this.money += money;
        this.updateUI();
        if (this.money >= this.moneyRequired[this.level]) {
            this.level = Number(this.level) + 1;
            if (this.level > this.moneyRequired.length - 1) {
                this.level = this.moneyRequired.length - 1;
            }
            cc.sys.localStorage.setItem('level', this.level);
            ShopItemManager.Instance(ShopItemManager).isStrengthUp = false;
            this.shopPanel.active = true;
        }
    }

    public updateUI() {
        this.levelLabel.string = this.level.toString();
        this.targetMoneyLabel.string = this.moneyRequired[this.level].toString();
        this.currentMoneyLabel.string = this.money.toString();
        this.timeLabel.string = GameManager.Instance(GameManager).remainTime.toString();
    }

    public gameOver() {
        this.endGamePanel.active = true;
        this.btnLabel.string = '重来';
        this.tipLabel.string = '失败了';
    }

    public playAnim(name: string = '') {
        let state = this.minerAnimation.play(name);
        state.wrapMode = cc.WrapMode.Loop;
    }

    public stopAnim() {
        this.minerAnimation.stop()
    }


}
